//
//  CZTabBarItem.swift
//  weibo
//
//  Created by Apple on 16/3/17.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

class CZTabBarItem: UITabBarItem {

    override var badgeValue: String? {
        
        didSet {
            if badgeValue == nil {
                return
            }
            
            //  获取target获取UITabbarController，其实就是CZMainViewController
            let tabBarController = valueForKey("_target") as! UITabBarController
            print(valueForKey("_target"))
            for tabBarChild in tabBarController.tabBar.subviews {
                //            print(tabBarChild)
                if tabBarChild.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                    for tabBarButtonChild in tabBarChild.subviews {
                        
                        if tabBarButtonChild .isKindOfClass(NSClassFromString("_UIBadgeView")!) {
                            
                            for badgeViewChild in tabBarButtonChild.subviews {
                                
                                if badgeViewChild.isKindOfClass(NSClassFromString("_UIBadgeBackground")!) {
                                    print("终于找到你，还好没有放弃")
                                    // 需要显示指定类型要不然会报警告
                                    //                                let a: AnyClass = NSClassFromString("_UIBadgeBackground")!
                                    //                                print(a)
                                    //  返回成员列表数量
                                    var count: UInt32 = 0
                                    //  获取类中的成员的列表
                                    let ivars = class_copyIvarList(NSClassFromString("_UIBadgeBackground")!, &count)
                                    
                                    for i in 0..<count {
                                        //  取出对应位置的值
                                        let ivar = ivars[Int(i)]
                                        //  获取成员变量的名字
                                        let name = ivar_getName(ivar)
                                        let nameStr = String(CString: name, encoding: NSUTF8StringEncoding)
                                        print(nameStr)
                                        
                                        let type = ivar_getTypeEncoding(ivar)
                                        let typeStr = String(CString: type, encoding: NSUTF8StringEncoding)
                                        print(typeStr)
                                        //  判断是image的话才设置kvc，如果不判断假如还有其他属性也设置成image了，防止崩溃
                                        if nameStr == "_image" {
                                            //
                                            badgeViewChild.setValue(UIImage(named: "main_badge"), forKey: nameStr!)
                                        }
                                        
                                        
                                        
                                    }
                                    
                                    
                                }
                                
                            }
                            
                            
                        }
                        
                    }
                }
            }

            
        }
        
    }
    
}
